const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: './src/js/index.js',
    output: {
        filename: "js/[name].js",
        path: path.resolve(__dirname, './dist')
    },
    module: {
        rules: [
            //js  4个  npm install babel-loader @babel/core @babel/preset-env webpack
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            //css
            {
                test: /\.scss$/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'sass-loader']
                })
            },
            //html
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        fliename: '[name].[ext]'
                    }
                }
            },
            // img
            {
                test: /\.(img|png|jpg)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        name: 'img/[name].[ext]',
                        limit: 5000,
                        publicPath: '/'
                    }
                }
            }
        ]
    },
    plugins: [
        //css
        new ExtractTextWebpackPlugin({
            filename: 'css/[name].css'
        }),
        new HtmlWebpackPlugin({
            // filename: './src/index.html',
            template: './src/index.html' //原本index.html的路径
        })
    ],
    devServer: {
        port: 3500,
        host: 'localhost',
        hot: true, //实时更新
        open: true, //自动打开浏览器
        before(app) {
            app.get('/api/list', (req, res, next) => {

            })
        }
    }
}